<?php
$this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'category-grid',
	'dataProvider'=>$model->category_search($alias),
	'selectableRows'=>2,
	'columns'=>array(
		array(
				'class' => 'CCheckBoxColumn',
				'htmlOptions' => array('class'=>'jform'),
				'headerHtmlOptions' => array('class' => 'jform jlb_checkbox_check_all jlb_checkbox_check_all_disable'),
				'header' => '<input type="checkbox" class="select-on-check-all"/>',
				'value' => '$data->primaryKey',
				'htmlOptions' => array('style' => 'width: 20px;text-align:center;'),
		),
		array(
				'name'=>'name',
				'htmlOptions' => array('style' => 'width: 200px;'),
		),
		array(
				'name' => 'image',
				'type' => 'html',
				//'value' => '(!empty($data->image) && file_exists(Yii::app()->createUrl("/")."/wwwroot/upload_files/events/thumb_".$data->image))? Yii::app()->createUrl("/")."/wwwroot/upload_files/events/thumb_".$data->image : Yii::app()->createUrl("/")."/wwwroot/upload_files/events/no-image.jpg"',
				
				'value'=>'(!empty($data->image) && file_exists(YiiBase::getPathOfAlias("webroot"). Yii::app()->controller->module->image["url"]."thumb_".$data->image))?CHtml::image(Yii::app()->createUrl("/").Yii::app()->controller->module->image["url"]."thumb_".$data->image, "", array("style" => "width: 100px; height: 70px;")) : CHtml::image(Yii::app()->createUrl("/").Yii::app()->controller->module->image["url"]."no-image.jpg", "", array("style" => "width: 100px; height: 70px;"))',
				
				'htmlOptions' => array('style' => 'width: 110px;'),
		),
	    //'alias',
	    //'description',
		array(
			'type'=>'raw',
			'name' => 'price',
			'value'=>'CHtml::activeTextField($data,"price",array("rel"=>$data->id,"style"=>"width:80px;"))',
			'htmlOptions'=>array('style'=>'width:100px;text-align:center;')
		),
		array(
			'name' => 'special_price',
			'type'=>'raw',
			'value'=>'CHtml::activeTextField($data,"special_price",array("rel"=>$data->id,"style"=>"width:80px;"))',
			'htmlOptions'=>array('style'=>'width:100px;text-align:center;')
		),
		array(
				'name' => 'description',
				'type' => 'html',
		),
		array(
			'name'=>'is_special',
			'class' => 'CDataColumn',
			'type'=>'raw',
			'value'=>'CHtml::activeCheckBox($data,"is_special",array("rel"=>$data->id))',
			'htmlOptions'=>array('style'=>'width:50px;text-align:center;')	
		),
		array(
			'class'=>'CButtonColumn', 
			'viewButtonUrl' => 'Yii::app()->createUrl("/products/manage_product/view",
			array("id" => $data->id))',
			'updateButtonUrl' => 'Yii::app()->createUrl("/products/manage_product/update",
			array("id" => $data->id))',
			'deleteButtonUrl' => 'Yii::app()->createUrl("/products/manage_product/delete",
			array("id" => $data->id))',
		),
	),
)); 
?>
<!-- <a href="#" onclick="return actionDelete();">Delete</a> | -->
<?php
echo CHtml::link('Create a news', array('/products/manage_product/create')); 
?>
<script type="text/javascript">
	$(document).ready(function(){
		$('[name="Products[is_special]"][type="checkbox"]').change(function(){
			var prdId = $(this).attr("rel");
			var isSpecial = $(this).prop("checked")?1:0;
			var data = {"Products":{'is_special':isSpecial}};
			changeProduct(data,prdId);
		});


		$('[name="Products[price]"]').change(function(){
			var prdId = $(this).attr("rel");
			var data = {"Products":{'price':$(this).val()}};
			changeProduct(data,prdId);
		});
		
		$('[name="Products[special_price]"]').change(function(){
			var prdId = $(this).attr("rel");
			var data = {"Products":{'special_price':$(this).val()}};
			changeProduct(data,prdId);
		});
	});

	function changeProduct(data,prdId){
		var url = '/products/manage_product/ajaxupdate/id/'+prdId;
		$.ajax({
			url:url,
			type:"POST",
			data:data,
			dataType:'html',
			success:function(svRps){
				console.log(svRps);
			},
			error:function(a,b,c){
				alert(c);
			}
		});
	}
</script>